package com.test.selenium.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class OutputSheet {
	private HSSFWorkbook workbook;
	private HSSFSheet outputSheet;
	private String outFileName;
	private FileOutputStream outFile;
	private String templatePath;
	
	public OutputSheet() throws FileNotFoundException, IOException {
		String currentDateAndTime = new SimpleDateFormat("MMM-dd-yyyy_HHmmss").format(new Date());
    	outFileName = "..\\selenium\\src\\Output\\SmokeTest-AirModeResults_" + currentDateAndTime + ".xls";
		openTemplate();
	}
	
	public void openNewSheet() throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet outputSheet = workbook.createSheet("Air Test Result");
		Row rowOut = outputSheet.createRow(0);
		rowOut.createCell(0).setCellValue("Area");
		rowOut.createCell(1).setCellValue("Test Case");
		rowOut.createCell(2).setCellValue("PASS/FAIL");
		rowOut.createCell(3).setCellValue("Value");
		rowOut.createCell(4).setCellValue("Time Taken(Secs)");
    	
    	outFile = new FileOutputStream(new File(outFileName));
		workbook.write(outFile);
	}
	
	private void openTemplate() throws FileNotFoundException, IOException {
		templatePath = "..\\selenium\\src\\Template\\SmokeTest_Output_Template.xls";
		workbook = new HSSFWorkbook(new FileInputStream(new File(templatePath)));
		outputSheet = workbook.getSheet("Test Results");
	}
	

	public void writeTestStepResult(int stepNumber, String testStepName, boolean isPass, String timeTaken, String value, String comment) throws IOException {
//		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(fileName)));
//		HSSFSheet outputSheet = workbook.getSheet("Air Test Result");
		System.out.println("Current test step number is " + stepNumber);
		Row rowToWrite = outputSheet.createRow(stepNumber);
		rowToWrite.createCell(0).setCellValue(testStepName);
		rowToWrite.createCell(1).setCellValue(isPass ? "Pass" : "Fail");
		rowToWrite.createCell(2).setCellValue(timeTaken);
		rowToWrite.createCell(3).setCellValue(value);
		rowToWrite.createCell(4).setCellValue(comment);
	}
	
	public void writeTestStepResult(int stepNumber, String testStepName, boolean isPass, String timeTaken, String value) throws IOException {
		this.writeTestStepResult(stepNumber, testStepName, isPass, timeTaken, value, null);
	}
	
	public void writeTestStepResult(int stepNumber, String testStepName, boolean isPass, String timeTaken) throws IOException {
		this.writeTestStepResult(stepNumber, testStepName, isPass, timeTaken, null, null);
	}
	
	public void writeToFile() throws IOException {
		File outXls = new File(outFileName);
		FileOutputStream outStream = new FileOutputStream(outXls);
		workbook.write(outStream);
	}
}
